package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "C端菜品接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    @ApiOperation("根据分类id 查询菜品以及口味")
    @Cacheable(cacheNames = "dishCache",key = "#categoryId")
    public Result list(Long categoryId) {
        log.info("查询菜品信息，分类id为{}", categoryId);
//        // 查询缓存
//        // 缓存id：dish_ + 分类id
//        String key ="dish_"+categoryId;
//        // 从缓存中获取数据    强转
//        List<DishVO> dishVoList = (List<DishVO>) redisTemplate.opsForValue().get(key);
//        // 判断缓存数据是否存在
//        if (dishVoList != null && dishVoList.size() > 0) {
//            return Result.success(dishVoList);
//        }
        // 如果不存在，则查询数据库
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(1); //只查询起售的
        List<DishVO> List =dishService.listWithFlavor(dish);
        //将数据 加入缓存
//        redisTemplate.opsForValue().set(key, List);
        return Result.success(List);
    }
}
